package link.cocoon.common.exception.handler;

import link.cocoon.common.resp.Resp;
import link.cocoon.common.resp.RespData;
import link.cocoon.common.exception.LoginException;
import link.cocoon.common.exception.RelationCheckException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author xce
 * @date 2019/10/23 15:29
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandle {

  /**
   * 参数校验异常拦截.
   * @param e 异常对象
   * @return Api返回结果
   */
  @ExceptionHandler( MethodArgumentNotValidException.class)
  public RespData<String> MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
    ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
    return RespData.error(objectError.getDefaultMessage());
  }

  @ExceptionHandler( RelationCheckException.class )
  public Resp dataCheckExceptionHandler(RelationCheckException e){
    e.printStackTrace();
    return RespData.error(e.getMessage());
  }

  @ExceptionHandler( LoginException.class )
  public RespData<String> loginExceptionHandler(LoginException e){
    e.printStackTrace();
    return RespData.error(e.getMessage());
  }


}
